﻿#include "config.h"
#include "tables.h"
#include "mylog.h"

Config::Config(QObject *parent) : QObject(parent)
{
    QString filePath = MyHelper::GetFilePath("data/conf.ini");//读取配置文件
    setting = new QSettings("data/conf.ini", QSettings::IniFormat);//第二个参数表示针对ini文件
    setting->setIniCodec("UTF-8");

    system = new System();
    database = new Database();
    serial = new Serial();

    this->LoadConfig();//根据配置文件初始化
    this->SaveConfig();//保存配置文件
}

void Config::LoadConfig(){
    system->filePath        = setting->value("/System/filePath").toString();
    system->localHost       = setting->value("/System/localHost").toString();
    system->httpPort        = setting->value("/System/httpPort").toInt();
    system->fileExpireDay   = setting->value("/System/fileExpireDay").toInt();
    system->dataExpireDay   = setting->value("/System/dataExpireDay").toInt();

//    database->ip            = setting->value("/Database/ip").toString();
//    database->port          = setting->value("/Database/port").toInt();
//    database->userName      = setting->value("/Database/userName").toString();
//    database->password      = setting->value("/Database/password").toString();
//    database->databaseName  = setting->value("/Database/databaseName").toString();

    serial->serialName       = setting->value("/SerialPort/serialName").toString();
    serial->baudRate         = setting->value("/SerialPort/baudRate").toInt();
    serial->dataBit          = setting->value("/SerialPort/dataBit").toInt();
    serial->checkoutBit      = setting->value("/SerialPort/checkoutBit").toInt();
    serial->stopBit          = setting->value("/SerialPort/stopBit").toInt();

    if(system->filePath.isEmpty())       system->filePath = "E:\\AudioData\\";
    if(system->localHost.isEmpty())      system->localHost = "127.0.0.1";
    if(setting->value("/System/httpPort").isNull()) system->httpPort = 8000;

    if(setting->value("/System/fileExpireDay").isNull()) system->fileExpireDay = 15;
    if(system->fileExpireDay <= 0 || system->fileExpireDay > 30) system->fileExpireDay = 30;

    if(setting->value("/System/dataExpireDay").isNull()) system->dataExpireDay = 180;
    if(system->dataExpireDay <= 0 || system->dataExpireDay > 365) system->dataExpireDay = 365;


    if(setting->value("/Database/ip").isNull()){
        database->ip            = "127.0.0.1";
        database->port          = 3306;
        database->userName      = "root";
        database->password      = "root";
        database->databaseName  = "MainTain";
    }

//    if(serial->serialName.isEmpty()){
//        serial->serialName = "COM1";
//        serial->baudRate = 9600;
//        serial->dataBit = 8;
//        serial->checkoutBit = 0;
//        serial->stopBit = 0;
//    }

    MyLog::Info("配置加载成功");
}

void Config::SaveConfig(){
    setting->setValue("/System/filePath", system->filePath);
    setting->setValue("/System/localHost",system->localHost);
    setting->setValue("/System/httpPort",system->httpPort);
    setting->setValue("/System/fileExpireDay",system->fileExpireDay);
    setting->setValue("/System/dataExpireDay",system->dataExpireDay);

    setting->setValue("/Database/ip", database->ip);
    setting->setValue("/Database/port", database->port);
    setting->setValue("/Database/userName", database->userName);
    setting->setValue("/Database/password", database->password);
    setting->setValue("/Database/databaseName", database->databaseName);

//    setting->setValue("/SerialPort/serialName", serial->serialName);
//    setting->setValue("/SerialPort/baudRate", serial->baudRate);
//    setting->setValue("/SerialPort/dataBit", serial->dataBit);
//    setting->setValue("/SerialPort/checkoutBit", serial->checkoutBit);
//    setting->setValue("/SerialPort/stopBit", serial->stopBit);

    emit Update();

    MyLog::Info("配置保存成功");
}
